package com.geek.novel.services

import android.content.res.Resources
import android.text.TextUtils
import com.geek.novel.bo.AppContext
import com.geek.novel.entity.BookIndex
import com.geek.novel.entity.BookSection
import com.vicpin.krealmextensions.delete
import com.vicpin.krealmextensions.query
import com.vicpin.krealmextensions.queryFirst
import com.vicpin.krealmextensions.save

class BookSectionRepository{
	
	private fun getResources(): Resources {
		return AppContext.getResources()
	}
	
	/**保存*/
	fun save(bookSection: BookSection){
		bookSection.save()
	}


	/**根据书本获取当前章节*/
	fun findSectionById(id: String?): BookSection?{
		if(TextUtils.isEmpty(id)){
			return null
		}
		return BookSection().queryFirst { equalTo("id", id) }
	}

	/**根据书本获取当前章节*/
	fun findSectionByIdAndBookCrawlerId(id: String?, bookCrawlerId: String?): BookSection?{
		if(TextUtils.isEmpty(id)){
			return null
		}
		return BookSection().queryFirst { equalTo("id", id).and().equalTo("bookCrawlerId", bookCrawlerId) }
	}

	/**
	 * 删除已经爬取过的章节
	 */
	fun deleteByBookId(bookId: String){
		BookSection().delete { equalTo("bookId", bookId) }
	}
	
	/**
	 * 删除已经爬取过的章节
	 */
	fun deleteByBookIdAndIndexId(bookId: String, indexId: Long){
		BookSection().delete { equalTo("bookId", bookId).and().equalTo("indexId", indexId) }
	}
	
}